<html>
<head><meta charset="utf-8"><title>Force runtime feature detection to happen at runtime · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Force.20runtime.20feature.20detection.20to.20happen.20at.20runtime.html">Force runtime feature detection to happen at runtime</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="195501602"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Force%20runtime%20feature%20detection%20to%20happen%20at%20runtime/near/195501602" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Force.20runtime.20feature.20detection.20to.20happen.20at.20runtime.html#195501602">(Apr 28 2020 at 04:55)</a>:</h4>
<p>Problem: <code>is_x86_feature_detected!</code> (and its friends) will simply compile to <code>true</code> if a feature is enabled at compile time, instead of actually checking if a feature is available at runtime.</p>
<p>This means that you can't have a program where you enable extra CPU features for the build and then check your assumptions at the start of the program and print a nice error message to the user if their CPU doesn't fit your assumptions.</p>
<p>I think we should support that sort of thing, so I think that we need an "actually really check for this feature at runtime" ability.</p>



<a name="195523796"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Force%20runtime%20feature%20detection%20to%20happen%20at%20runtime/near/195523796" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Force.20runtime.20feature.20detection.20to.20happen.20at.20runtime.html#195523796">(Apr 28 2020 at 06:39)</a>:</h4>
<p>This might be a nice mitigation for some programs, but since e.g. enabling AVX will let LLVM use AVX all over the code for small-ish memcpys, it's generally only useful if done <em>immediately</em> at the start of <code>main()</code>. This is specific enough that I don't think it should be tied to <code>is_x86_feature_detected</code>, it should be a separate function for this specific use case only.</p>



<a name="195525917"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Force%20runtime%20feature%20detection%20to%20happen%20at%20runtime/near/195525917" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Force.20runtime.20feature.20detection.20to.20happen.20at.20runtime.html#195525917">(Apr 28 2020 at 07:09)</a>:</h4>
<p>I would absolutely agree. It should be stand alone.</p>
<p>If it were to eventually be in libstd, probably the best way would be to expose the "backend" of the macro that does the runtime checking, and then the macro would continue to wrap it all up in a <code>cfg!(target_feature="foo") || runtime_check("foo")</code> type of deal.</p>



<a name="195529730"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Force%20runtime%20feature%20detection%20to%20happen%20at%20runtime/near/195529730" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Force.20runtime.20feature.20detection.20to.20happen.20at.20runtime.html#195529730">(Apr 28 2020 at 07:56)</a>:</h4>
<p>In the general case, I would expect a program compiled with such support enabled to potentially attempt to use that support <em>before</em> main, or for that matter in the course of calling ordinary functions, unpredictably.</p>



<a name="195529735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Force%20runtime%20feature%20detection%20to%20happen%20at%20runtime/near/195529735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Force.20runtime.20feature.20detection.20to.20happen.20at.20runtime.html#195529735">(Apr 28 2020 at 07:56)</a>:</h4>
<p>So even checking at the top of main may not suffice.</p>



<a name="195567653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Force%20runtime%20feature%20detection%20to%20happen%20at%20runtime/near/195567653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Force.20runtime.20feature.20detection.20to.20happen.20at.20runtime.html#195567653">(Apr 28 2020 at 13:58)</a>:</h4>
<p>I think <span class="user-mention" data-user-id="132920">@gnzlbg</span> had a issue or thread about automatically doing this (and maybe e.g. panicking or whatever if the feature set doesn't match)</p>



<a name="195581956"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Force%20runtime%20feature%20detection%20to%20happen%20at%20runtime/near/195581956" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Force.20runtime.20feature.20detection.20to.20happen.20at.20runtime.html#195581956">(Apr 28 2020 at 15:35)</a>:</h4>
<p>What about intrinsics?<br>
Can we not use avx intrinsics without compiling everything with avx enabled? (and then guard those with a runtime check)</p>



<a name="195590198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Force%20runtime%20feature%20detection%20to%20happen%20at%20runtime/near/195590198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Force.20runtime.20feature.20detection.20to.20happen.20at.20runtime.html#195590198">(Apr 28 2020 at 16:31)</a>:</h4>
<p>Uh, right. To back up a bit and give a little background: <code>core::arch</code> exposes all intrinsics as <code>unsafe</code> functions without you needing to enable the feature at compile time. It's up to you to come up with a system of making the unsafe safe.</p>
<ul>
<li>One way is with runtime checks and then calling specific functions that are designed to utilize a feature once you know that the feature is available on the current machine.</li>
<li>The other is by turning on compile time target features and then having LLVM just use the features wherever it thinks they're appropriate all over the place (and you can also manually call them yourself of course).</li>
</ul>
<p>The second option is "simpler", but also now your program expects a minimum CPU to run, and both Rust and most operating systems lack a way to mark a binary with what CPU features it needs, so there's no easy way to give a message if the current CPU is missing a feature you expected. Instead the user runs a program that looks like a normal program, and they get a mysterious crash.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>